<?php
tsload ( APPS_PATH . '/admin/Lib/Action/AdministratorAction.class.php' );
class PackAction extends AdministratorAction {
	
	public function index(){//【套餐列表】
		$this->_initPackAdminMenu();
		$map = array();
		$this->pageKeyList = array('pack_id','title','intro','price','click','days','ctime','status','DOACTION');
		
		$this->_listpk = 'pack_id';
		$listData = $this->getPackList(20,$map);
		$this->displayList($listData);
	}
	
	public function getPackList($limit,$map){
		$list = M("Advert_pack")->where($map)->order("pack_id desc")->findPage($limit);
		
		foreach($list['data'] as $k => $v) {
		
			$list['data'][$k]['ctime'] = $v['ctime']=='0'?'------':date("Y-m-d h:i:s",$v['ctime']);
			$list['data'][$k]['status'] = $v['status']=='0'?'关闭中':'使用中';
			$list['data'][$k]['DOACTION'] = $v['status'] == 0 ? '<a href="javascript:void(0)" onclick="admin.rebackPack(\''.$v['pack_id'].'\')">'.'启用'.'</a> - ' : '<a href="javascript:void(0)" onclick="admin.disPack(\''.$v['pack_id'].'\')">'.'关闭'.'</a> - ';
			$list['data'][$k]['DOACTION'] .= '<a href="'.U('admin/Pack/editPack',array('id'=>$v['pack_id'],'tabHash'=>'editPack')).'">修改</a>-<a onclick="admin.delPack('.$v['pack_id'].');" href="javascript:void(0)">删除</a>';

		}
		
		return $list;
	
	}
	
	
	public function addPack(){//【添加套餐】
		$this->_initPackAdminMenu();
		$this->pageKeyList = array('title','price','intro','click','days','status');
		$this->opt['days'] = array_merge(array('0'=>'选择天数'),range(1,120));
		$this->opt['status'] = array('0'=>'关闭','1'=>'使用');
		
		$this->savePostUrl = U('admin/Pack/doAddPack');
        $this->notEmpty = array("title","price");
		$this->onsubmit = 'admin.addPackSubmitCheck(this)';
		
		$this->displayConfig();
	
	}
	
	public function doAddPack(){//【执行添加套餐】
		$Advert_pack = M("Advert_pack");
		$Advert_pack->title = $title = $_REQUEST['title'];
		$Advert_pack->price = $price= $_REQUEST['price'];
		$Advert_pack->intro = $intro = $_REQUEST['intro'];
		$Advert_pack->click = $click = $_REQUEST['click'];
		$Advert_pack->days = $days = $_REQUEST['days'];
		$Advert_pack->status = $status = $_REQUEST['status'] == '1'?'1':'0';
		$Advert_pack->ctime = time();
		$check = $Advert_pack->where("title = '$title'")->find();
		if($check){
			$this->assign('jumpUrl', U('admin/Pack/addPack'));
			$this->error('套餐名已存在');
		}
		$result = $Advert_pack->add();
		if($result){
			$this->assign('jumpUrl', U('admin/Pack/index'));
			$this->success(L('PUBLIC_ADD_SUCCESS'));
		}else{
			$this->assign('jumpUrl', U('admin/Pack/addPack'));
			$this->error('添加失败');
		}
		
	}
	
	public function editPack(){//【修改套餐】
		$this->_initPackAdminMenu();
		$Advert_pack = M("Advert_pack");
		$pack_id = $_REQUEST['id'];
		$data = $Advert_pack->where("pack_id = $pack_id")->find();
		$this->pageKeyList = array('pack_id','title','price','intro','click','days','status');
		$this->opt['days'] = array_merge(array('0'=>'选择天数'),range(1,120));
		$this->opt['status'] = array('0'=>'关闭','1'=>'使用');
		
		$this->savePostUrl = U('admin/Pack/doEditPack');
        $this->notEmpty = array("title","price");
		$this->onsubmit = 'admin.addPackSubmitCheck(this)';
		$this->displayConfig($data);
	}
	
	public function doEditPack(){//【执行修改套餐】
		
		$Advert_pack = M("Advert_pack");
		$pack_id = $_REQUEST['pack_id'];
		$Advert_pack->title = $title = $_REQUEST['title'];
		$Advert_pack->price = $price= $_REQUEST['price'];
		$Advert_pack->intro = $intro = $_REQUEST['intro'];
		$Advert_pack->click = $click = $_REQUEST['click'];
		$Advert_pack->days = $days = $_REQUEST['days'];
		$Advert_pack->status = $status = $_REQUEST['status'] == '1'?'1':'0';
		
		$map['title'] = array('eq',$title);
		$map['pack_id'] = array('neq',$pack_id);
		$check = $Advert_pack->where($map)->find();
		if($check){
			$this->assign('jumpUrl', U('admin/Pack/editPack',array('id'=>$pack_id,'tabHash' => 'editPack')));
			$this->error('套餐名已存在');
		}
		$result = $Advert_pack->where("pack_id = $pack_id")->save();
		if($result){
			$this->assign('jumpUrl', U('admin/Pack/editPack',array('id'=>$pack_id,'tabHash' => 'editPack')));
			$this->success(L('修改成功'));
		}else{
			$this->assign('jumpUrl', U('admin/Pack/editPack',array('id'=>$pack_id,'tabHash' => 'editPack')));
			$this->error('修改失败');
		}
	
	}
	
/**
	 * 套餐关闭使用操作
	 * @return json 操作后的JSON数据
	 */
	public function disPack() {
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode($return));
		}

		//$result = model('User')->deleteUsers(intval($_POST['id']));
		$Adert_pack = M('Advert_pack');
		$Adert_pack->status = 0;
		$Adert_pack->pack_id  =$_POST['id'];
		$result = $Adert_pack->save();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');				// 操作失败
		} else {
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = 1;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');			// 操作成功
			
		}
		exit(json_encode($return));
	}

	/**
	 * 套餐恢复启用操作
	 * @return json 操作后的JSON数据
	 */
	public function rebackPack() {
		if(empty($_POST['id'])) {
			$return['status'] = 0;
			$return['data'] = '';
			exit(json_encode($return));
		}
		$Adert_pack = M('Advert_pack');
		$Adert_pack->status = 1;
		$Adert_pack->pack_id  =$_POST['id'];
		$result = $Adert_pack->save();
		if(!$result) {
			$return['status'] = 0;
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_ERROR');				// 操作失败
		} else {
			// 关联删除用户其他信息，执行删除用户插件.
			$return['status'] = '1';
			$return['data'] = L('PUBLIC_ADMIN_OPRETING_SUCCESS');			// 操作成功
		}
		exit(json_encode($return));
	}
	
	
	
	private function _initPackAdminMenu(){
		$this->pageTab[] = array('title'=>'套餐列表','tabHash'=>'index','url'=>U('admin/Pack/index'));
		$this->pageTab[] = array('title'=>'添加套餐','tabHash'=>'addpack','url'=>U('admin/Pack/addPack'));
	}
	//申请管理
	
	private function _initApplyAdminMenu(){
		$this->pageTab[] = array('title'=>'申请列表','tabHash'=>'index','url'=>U('admin/Pack/apply'));
		//$this->pageTab[] = array('title'=>'添加套餐','tabHash'=>'addshop','url'=>U('admin/Pack/addPack'));
	}
	
	public function apply(){//【申请列表】
		$this->_initApplyAdminMenu();
		$map = array();
		$this->pageKeyList = array('id','pack_id','pid','money','pay_id','channel','order_no','subject','body','status');
		
		$this->_listpk = 'id';
		$listData = $this->getApplyList(20,$map);
		$this->displayList($listData);
	}
	
	public function getApplyList($limit,$map){
		$list = M("Advert_pack_record")->where($map)->order("id desc")->findPage($limit);
		
		foreach($list['data'] as $k => $v) {
//		
//			$list['data'][$k]['ctime'] = $v['ctime']=='0'?'------':date("Y-m-d h:i:s",$v['ctime']);
//			$list['data'][$k]['status'] = $v['status']=='0'?'关闭中':'使用中';
//			$list['data'][$k]['DOACTION'] = $v['status'] == 0 ? '<a href="javascript:void(0)" onclick="admin.rebackPack(\''.$v['pack_id'].'\')">'.'启用'.'</a> - ' : '<a href="javascript:void(0)" onclick="admin.disPack(\''.$v['pack_id'].'\')">'.'关闭'.'</a> - ';
//			$list['data'][$k]['DOACTION'] .= '<a href="'.U('admin/Pack/editPack',array('id'=>$v['pack_id'],'tabHash'=>'editPack')).'">修改</a>-<a onclick="admin.delPack('.$v['pack_id'].');" href="javascript:void(0)">删除</a>';

		}
		
		return $list;
	
	}
}